<?php 

if($_GET['product']!='new'){
	$params=array($_GET['product']);
		// get product
	$product = $shopObj->fetch_one("SELECT products.*,products_details.* FROM products_details,products WHERE url =? AND products.id=products_details.product_id",$params);
			
		// get product details
	
	$products_det = $shopObj->fetch_result("SELECT * FROM products_details WHERE product_id={$product['product_id']}");
	$product_comm = $shopObj->fetch_result("SELECT comments.* FROM comments,products_comments WHERE products_comments.comment_id = comments.id AND products_comments.product_id='{$product['id']}' ");
	foreach($products_det as $value){
		$products_details[$value['language_id']]=$value;
	}

		// get product categories
	$cats = $shopObj->fetch_result("SELECT category_id FROM products_categories WHERE product_id={$product['product_id']}");
	foreach ($cats as $key => $value) {
		$products_categories[]=$value['category_id'];
	}
	if($product['in_footer']==1){
		$infooter = ' checked="checked" ';
	}
	if($product['has_liqvidation']==1){
		$has_liqvidation=' checked="checked" ';
	}
	if($product['visible']==1){
		$visible = ' checked="cheched" ';
	}
	
}
if(!isset($visible)){
	$visible = ' checked="checked" ';
}




?>
<div class="tabbable">
	<ul class="nav nav-tabs" id="site_page_tabs">
		<li class='active' >
			<a data-toggle="tab" href="#details">
				<?php  echo translate_word_shop('Product details',$dictionary); ?>
			</a>
		</li>
		<li class='' >
			<a data-toggle="tab" href="#stocks" id='stocks_head_tab'>
				<?php  echo translate_word_shop('Product stocks',$dictionary_app); ?>
			</a>
		</li>
		<li class='' >
			<a data-toggle="tab" href="#comments" id='comments_head_tab'>
				<?php  echo translate_word_shop('Comments',$dictionary_app); ?>
			</a>
		</li>
	</ul>
	<div class="tab-content" style='height:100%'>
		<div id='details' class="tab-pane active" style='height:100%;overflow:hidden' >

			<div class='row-fluid'>
				<form id='product_descriptions' name='product_descriptions' method='get' action=''>
					<div class='span3'>
						<ul class="unstyled spaced2">
							<li>
								<i class="icon-circle green"></i>
								completeaza datele produsului tau pentru fiecare limba selectata
							</li>
							
						</ul>	
					</div>	

					<div class="tabbable span9">
						<ul class="nav nav-tabs" id="site_page_description">
							<?php 
							$ii=0;
							foreach($available_lang as $value){
								if($ii==0){
									$active='active';
								}else{
									$active='';
								}
								echo '<li class="'.$active.'" >
								<a data-toggle="tab" href="#description_'.$value['id'].'">
								'.$value['abv'].'
								</a>
								</li>';
								$ii++;
							}
							?>
						</ul>


						<div class="tab-content" style='background-color:white'>				

							<?php
							$i=0;
							foreach($available_lang as $value){ 
								if($i==0){
									$active_tab='active';
								}else{
									$active_tab='';
								}
								$i++;
								$params=array($page['id'],$value['id']);
								$page_details=$shopObj->fetch_one("SELECT * FROM site_page_details WHERE site_page_id=? AND language_id=?",$params);

								?>
								<div id='description_<?php echo $value['id']; ?>' class='tab-pane <?php echo $active_tab; ?>'>
									<div class="control-group">
										<label class="control-label span2" for="name_<?php echo $value['id'] ?>"><?php  echo translate_word('Product name',$dictionary_app); ?></label>
										<div class="controls span10">
											<input id="product_name_<?php echo $value['id'] ?>" name='name_<?php echo $value['id'] ?>' class='span5' type="text" placeholder="" value='<?php echo @$products_details[$value['id']]['name']; ?>'>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label span2" for="meta_keyword<?php echo $value['id'] ?>"><?php  echo translate_word('Meta keywords',$dictionary_app); ?></label>
										<div class="controls span10">
											<input id="meta_keywords_<?php echo $value['id'] ?>" name='meta_keywords_<?php echo $value['id'] ?>' class='span5' type="text" placeholder="" value='<?php echo  @$products_details[$value['id']]['meta_keywords']; ?>'>
										</div>
									</div>
									<br class='clear'/>
									<div class="control-group">
										<label class="control-label span2" for="meta_description<?php echo $value['id'] ?>" ><?php  echo translate_word('Meta description',$dictionary_app); ?></label>
										<div class="controls span10">
											<textarea id='meta_description_<?php echo $value['id'] ?>' name='meta_description_<?php echo $value['id'] ?>' class='span5'><?php echo @$products_details[$value['id']]['meta_description']; ?></textarea>
										</div>
									</div>
									<br class='clear'/>

									<div class="control-group">
										<label class="control-label span2" for="description"><?php  echo translate_word('Description',$dictionary_app); ?></label>
										<div class="controls span10">
											<div id="description_<?php echo $value['id'];  ?>" class="wysiwyg-editor header-color-blue" contenteditable="true">
												<?php echo @$products_details[$value['id']]['description'] ?>
											</div>
										</div>
									</div>


								</div>

								<?php } ?>

							</div>
							
						</div>
					</form>
				</div>
				<div class='row_fluid' style='margin-top:10px'>
					<form id='attributes_form' name='attributes_form' method='get' action=''>					
						<div class='span3'>
							<ul class="unstyled spaced2">
								<li>
									<i class="icon-circle green"></i>
									alege o categorie din care face parte produsul tau
								</li>
								<li>
									<i class="icon-circle green"></i>
									alege daca tipul produsului 
								</li>
								<li>
									<i class="icon-circle green"></i>
									bifeaza daca vrei ca produsul tau sa apara la lichidare, in subsol sau sa fie vizibil in magazinul tau online
								</li>
								<li>
									<i class="icon-circle green"></i>
									selecteaza sau adauga atributele si valorile atributelor produsului tau.
								</li>
								<li>
									<i class="icon-circle green"></i>
									pentru a adauga un nou atribut produsului click pe butonul "adauga atribut"
								</li>
								<li>
									<i class="icon-circle green"></i>
									pentru a adauga o noua proprietate sau valori proprietatilor click pe "adauga proprietate"  respectiv pe "adauga valoare"
								</li>
								
							</ul>
						</div>	
						<div class='span9'>
							<div class="control-group">
								<label class='control-label span2' for="site_category"><?php  echo translate_word_shop('Category',$dictionary); ?></label>
								<div class='controls span10'>
									<div class='span5'>
										<select multiple="" class="chzn-select" id="category"   data-placeholder="Choose a category...">
											<?php $categories = $shopObj->fetch_result("SELECT categories.id,category_details.name FROM categories 
												JOIN category_details on categories.id=category_details.category_id
												WHERE category_details.language_id='{$language[0]['id']}'"); 


											foreach ($categories as $key => $value) {
												echo '<option value="'.$value['id'].'"';
												if(in_array($value['id'], $products_categories)){
													echo ' selected="selected" ';	
												}
												echo '  >'.$value['name'].'</option>';
											}
											?>
										</select>

									</div>
									<div class='span7 pull-left'>
										<span class="btn btn-info btn-small tooltip-info  icon-info-sign " title="" data-placement="top" data-rel="tooltip" data-original-title="fill your shop name"></span>
									</div>
								</div>
								<br class='clear'/>
							</div>
							<br class='clear'/>
							<div class="control-group">
								<label class='control-label span2' for="site_category"><?php  echo translate_word_shop('Type',$dictionary); ?></label>
								<div class='controls span10'>
									<div class='span5'>
										<select   id="type" name='type' value='<?php echo $product['type']; ?>';   data-placeholder="Choose a type...">
											<option value='0'>none</option>
											<option value='1'>New product</option>
											<option value='2'>Comming soon</option>
										</select>

									</div>
									<div class='span7 pull-left'>
										<span class="btn btn-info btn-small tooltip-info  icon-info-sign " title="" data-placement="top" data-rel="tooltip" data-original-title="fill your shop name"></span>
									</div>
								</div>
								<br class='clear'/>
							</div>
							<div class="control-group">
								<label class='control-label span2'><?php  echo translate_word_shop('has_liqudation',$dictionary); ?></label>
								<div class='controls span10'>
									<div class='span5'>
										<input name='has_liqvidation' id='has_liqvidation' <?php echo $has_liqvidation; ?> type="checkbox" value="1">
										<span class="lbl"></span>
									</div>
									<div class='span7 pull-left'>
										<span class="btn btn-info btn-small tooltip-info  icon-info-sign " title="" data-placement="top" data-rel="tooltip" data-original-title="fill your shop name"></span>
									</div>
								</div>
								<br class='clear'/>
							</div>	
							<br class='clear'/>
							<div class="control-group">
								<label class='control-label span2'><?php  echo translate_word_shop('in_footer',$dictionary); ?></label>
								<div class='controls span10'>
									<div class='span5'>
										<input name='in_footer' id='in_footer' <?php echo $in_footer; ?> type="checkbox" value="1">
										<span class="lbl"></span>
									</div>
									<div class='span7 pull-left'>
										<span class="btn btn-info btn-small tooltip-info  icon-info-sign " title="" data-placement="top" data-rel="tooltip" data-original-title="fill your shop name"></span>
									</div>
								</div>
								<br class='clear'/>
							</div>	
							<br class='clear'/>
							<div class="control-group">
								<label class='control-label span2'><?php  echo translate_word_shop('visible',$dictionary); ?></label>
								<div class='controls span10'>
									<div class='span5'>
										<input name='visible' id='visible' <?php echo $visible; ?> type="checkbox" value="1">
										<span class="lbl"></span>
									</div>
									<div class='span7 pull-left'>
										<span class="btn btn-info btn-small tooltip-info  icon-info-sign " title="" data-placement="top" data-rel="tooltip" data-original-title="fill your shop name"></span>
									</div>
								</div>
								<br class='clear'/>
							</div>	
						
							<div id='attr_wrapper'>
								<?php

									// make the selects for attributes

								$attributes = $shopObj->fetch_result("SELECT properties.id,properties.type, properties_details.name FROM properties 
									JOIN properties_details ON properties.id =properties_details.property_id  
									WHERE properties_details.language_id='{$language[0]['id']}'");

								$products_prop = $shopObj->fetch_result("SELECT product_properties.*,properties_details.name FROM product_properties,properties_details WHERE 
																	product_id='{$product['product_id']}' AND properties_details.language_id={$language[0]['id']} AND properties_details.property_id=product_properties.property_id ");
								
								if(count($products_prop)>0){
									foreach ($products_prop as $key_selected => $value_selected) { ?>
										<br class='clear'/>
										<hr />
										<div class="control-group span12">
											<label class='control-label span2' for="site_category">Select attributes</label>
											<div class='controls span4'>
												<select class='select_attribute' onchange ='show_attributes(this.value,this)'>	
													<option></option>								
													<?php 											
													foreach ($attributes as $key => $value) {
														echo "<option value='{$value['id']}'";
														if($value['id']==$value_selected['property_id']){
															echo " selected='selected' ";
															$selected_id = $value_selected['id'];
															$selected_val =$value_selected['property_id'];
														}
														echo " >{$value['name']}</option>";
													}

													?>
												</select>	
												<br />
												<button  class="add_property btn btn-minier btn-success" >+Add property</button>
											</div>
											<div class='span6'>
												<?php 
													// get all property values 
											
													$params = array($selected_val,$available_lang[0]['id']);
													$properties = $shopObj->fetch_result("SELECT properties_values.id, properties_values_details.name  FROM properties_values,properties_values_details WHERE properties_values.property_id=?
														AND properties_values.id = properties_values_details.property_value_id AND language_id=?",$params);
													
													// get the seleected properties values for this product
													$selected_properties_vals = $shopObj->fetch_result("SELECT property_value_id FROM product_properties_values WHERE product_property_id = {$selected_id}");
													foreach($selected_properties_vals as $prop_value){
														$sel_vals[]= $prop_value['property_value_id'];
													}
															
													echo '<div class="control-group">
													<label class="control-label span3" for="properties_'.$_POST['get_properties'].'">Select values</label>
													<div class="controls span9">';
													echo '<select multiple="" class="chzn-select" name="properties_'.$_POST['get_properties'].'"   data-placeholder="Choose properties values...">';
													foreach ($properties as $key => $value) {
														echo "<option value='{$value['id']}'";
														if(in_array($value['id'], $sel_vals)){
															echo " selected='selected' ";
														}
														echo ">{$value['name']}</option>";
													}
													echo '</select><br /><button class="add_property_value btn btn-minier btn-success" style="margin-top:10px">+Add property value</button>';
													echo "</div></div>";
												?>
											</div>

										</div>
										
									<?php }
								}else{
									?>


									<div class="control-group span12">
										<label class='control-label span2' for="site_category">Select attributes</label>
										<div class='controls span4'>
											<select class='select_attribute' onchange ='show_attributes(this.value,this)'>	
												<option></option>								
												<?php 											
												foreach ($attributes as $key => $value) {
													echo "<option value='{$value['id']}' >{$value['name']}</option>";
												}

												?>
											</select>	
											<br />
											<button  class="add_property btn btn-minier btn-success" >+Add property</button>
										</div>
										<div class='span6'>

										</div>

									</div>
									<?php } ?>
								</div>
								<br class='clear'/>
								<hr />
								<button class='btn btn-primary btn-success' id='add_attribute'>+Add another attribute</button>

							</div>
							<button class="btn btn-primary pull-right" onclick = 'save_product();return false'>
								<i class="icon-save "></i>
								Save</button>
							</form>
						</div>




					</div>
					<div id='stocks' class="tab-pane ">
						<div class='span3'>
							<ul class="unstyled spaced2">
								<li>
									<i class="icon-circle green"></i>
									pentru a adauga un stoc produsului click pe "adauga stock"
								</li>
								<li>
									<i class="icon-circle green"></i>
									pentru a edita un stoc poti modifica valorile din tabel apartinand fiecarui stock in functie de ce proprietati ai selectat la pasul anterior
								</li>
								<li>
									<i class="icon-circle green"></i>
									poti sterge un stoc apasand pe butonul din dreapta fiecarui stock, sau poti bifa mai multe si le poti sterge pe toate apasand pe "Sterge bifate"
								</li>
								
							</ul>	
						</div>	
						<div class='span9'>
							<button class='btn btn-primary btn-success' onclick='add_stock(<?php echo $product['product_id']; ?>);return false;'>+Add stock</button>
							<div id='stocks_wrapper'>
								<?php

									echo '<table id="stocks_table" class="table table-striped table-bordered table-hover">
											<thead>
												<tr>	
													<th><a href="javascript:void(0)" onclick="delete_selected()">'.translate_word_shop('Delete selected',$dictionary).'</a></th>
													<th>'.translate_word_shop('Barcode',$dictionary).'</th>';
									foreach ($products_prop as $key => $value) {
										echo "<th>".ucfirst($value['name'])."</th>";
									}				
									echo		   '<th>'.translate_word_shop('Quantity',$dictionary).'</th>
													<th>'.translate_word_shop('Price',$dictionary).'</th>
													<th>'.translate_word_shop('Actions',$dictionary).'</th>
												</tr>
											<thead>
											<tbody>';
									$stocks = $shopObj->fetch_result("SELECT * FROM stocks WHERE product_id='{$product['product_id']}'");

									foreach($stocks as $key=>$value){
										$properties_array = json_decode($value['properties'],true);
										echo "<tr>
												<td><input type='checkbox'  class='delete_selected' value='{$value['id']}'/><span class='lbl'></span></td>
												<td><input type='text'  style='width:100px' value='{$value['barcode']}' onblur='update_stock(\"barcode\",{$value['id']},this);' /></td>";

												foreach ($products_prop as $key_prop => $value_prop) {
													echo "<td>";
													$params = array($value_prop['property_id'],$available_lang[0]['id']);
													$properties = $shopObj->fetch_result("SELECT properties_values.id, properties_values_details.name  FROM properties_values,properties_values_details WHERE properties_values.property_id=?
																						AND properties_values.id = properties_values_details.property_value_id AND language_id=?",$params);
													
													echo "<select style='width:100px' class='stock_properties' id='{$value_prop['property_id']}' onchange='update_stock_property({$value['id']},this)'>";
													echo "<option></option>";
													foreach($properties as $property_key=>$property_value){
														echo "<option value='{$property_value['id']}' ";
														if($property_value['id'] == $properties_array[$value_prop['property_id']]){
															 echo "selected='selected'";
														}
														echo " >{$property_value['name']}</option>";
													}

													echo "</select>
															</td>";
												}		

										echo	"
												
												<td><input type='text' style='width:50px' onblur='update_stock(\"quantity\",{$value['id']},this);' value='{$value['quantity']}' /></td>
												<td><input type='text' style='width:50px' onblur='update_stock(\"price\",{$value['id']},this);'  value='{$value['price']}' /></td>
												<td>
													<a class='red' href='javascript:void(0)' onclick='delete_stock({$value['id']},this)'>
														<i class='icon-trash bigger-130'></i>
													</a>
												</td>	
											 </tr>";
									}
									echo   '</tbody>		
										</table>';

								 ?>
							</div>
						</div>	
					</div>
					<div id='comments' class="tab-pane" style='height:100%;overflow:hidden' >
						<div class='row-fluid'>	
							<?php 
								echo '<table id="stocks_table" class="table table-striped table-bordered table-hover">
											<thead>
												<tr>
												<th>Name</th>
												<th>Email</th>
												<th>Date</th>
												<th>Visble</th>
												<th>Comments</th>
												<th>Actions</th>
												';
											
								echo "</tr>
										</thead>
										<tbody>";
									foreach ($product_comm as $key => $value) {
										
										if($value['visible']==1){
											$visible = " checked='true' ";
										}else{
											$visible=" checked='false' ";
										}
										$date = date("d-m-Y",$product_comm['date']);
												
										echo "<tr>
												<td>{$value['name']}</td>
												<td>{$value['email']}</td>
												<td>{$date}</td>
												<td>{$value['content']}</td>
												<td><input type='checkbox' name='visible' {$visible} id='visible' onclick='make_visible({$value['id']},this);'><span class='lbl'></span></td>
												<td>	
													<a class='red' href='javascript:void(0)' onclick='delete_comment({$value['id']},this)'>
														<i class='icon-trash bigger-130'></i>
													</a>
												</td>


											 </tr>";
									}
								echo "</tbody>
									</table>";
							?>
						</div>
					</div>
				</div>
			</div>	
			<!-- modal for attributes  -->

			<div id="modal-attributes" class="modal hide fade"  tabindex="-1">
				<div class="modal-header no-padding">
					<div class="table-header">
						<button type="button" class="close" data-dismiss="modal">&times;</button>
						Add new attribute

					</div>
				</div>	
				<br class='clear'/>
				<form id='properties_form' style='margin-left:20px' >	
					<div class="control-group ">
						<label class="control-label span3" for="att_type"><?php  echo translate_word_shop('Attribute type',$dictionary); ?></label>
						<div class="controls span9">
							<select id='att_type' name='att_type'>
								<option value='1'>text</option>
								<option value='2'>color</option>
								<option value='4'>icon</option>							
							</select>
						</div>
					</div>	

					<?php

					foreach($available_lang as $key=>$value){ ?>


					<div class="control-group ">
						<label class="control-label span3" for="att_name_<?php echo $value['id'] ?>">Atribute name <?php echo $value['abv']; ?></label>
						<div class="controls span9">
							<input id="att_name_<?php echo $value['id'] ?>" name='att_name_<?php echo $value['id'] ?>' class='span5' type="text" placeholder="" >
						</div>
					</div>
					<br class='clear'/>


					<?php 
				}
				?>
			</form>

			<div class="modal-footer ">
				<button class="btn btn-small btn-primary" data-dismiss="modal" onclick='save_attribute();' >Save</button>
				<button class="btn btn-small btn-danger btn-primary" data-dismiss="modal">Cancel</button>
			</div>
			<input type='hidden' value='<?php echo $product['product_id'] ?>' id='product' name='product'>
		</div>

			<!-- modal for properties  -->

			<div id="modal-properties" class="modal hide fade"  tabindex="-1">
				<div class="modal-header no-padding">
					<div class="table-header">
						<button type="button" class="close" data-dismiss="modal">&times;</button>					
						<?php  echo translate_word_shop('Add new property',$dictionary); ?>
					</div>
				</div>	
				<br class='clear'/>
				<form id='properties_values_form' style='margin-left:20px' >	
					<div class="control-group ">
						<label class="control-label span3" for="att_type"><?php  echo translate_word_shop('Select attribute',$dictionary); ?></label>
						<div class="controls span9">
							<select id='att_select' name='att_select' value='0'>								
								<option value='0' ></option>								
								<?php 											
								foreach ($attributes as $key => $value) {
									echo "<option value='{$value['id']}'";									
									echo " >{$value['name']}</option>";
								}

								?>
																
							</select>
						</div>
					</div>	

					<?php

					foreach($available_lang as $key=>$value){ ?>


					<div class="control-group ">
						<label class="control-label span3" for="property_name_<?php echo $value['id'] ?>"><?php  echo translate_word_shop('property_name',$dictionary); ?> <?php echo $value['abv']; ?></label>
						<div class="controls span9">
							<input id="property_name_<?php echo $value['id'] ?>" name='property_name_<?php echo $value['id'] ?>' class='span5' type="text" placeholder="" >
						</div>
					</div>
					<br class='clear'/>


					<?php 
				}

				
				
				?>
				<div class="control-group ">
					<label class="control-label span3" for="property_name_<?php echo $value['id'] ?>"><?php  echo translate_word_shop('property_value',$dictionary); ?> </label>
					<div class="controls span9">
						<input id="property_value" name='property_value' class='span5' type="text" placeholder="" >
					</div>
				</div>
				<br class='clear'/>
			</form>

			<div class="modal-footer ">
				<button class="btn btn-small btn-primary" data-dismiss="modal" onclick='save_property();' >Save</button>
				<button class="btn btn-small btn-danger btn-primary" data-dismiss="modal">Cancel</button>
			</div>
			<input type='hidden' value='<?php echo $product['product_id'] ?>' id='product' name='product'>
		</div>